package org.roguelikedevelopment.dweller.j2me;

import java.io.DataInputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;

import org.roguelikedevelopment.dweller.common.application.DataStorage;
import org.roguelikedevelopment.dweller.common.application.DwellerCanvas;
import org.roguelikedevelopment.dweller.common.application.DwellerApplication;
import org.roguelikedevelopment.dweller.common.game.GameCommand;
import org.roguelikedevelopment.dweller.common.game.GameHandler;
import org.roguelikedevelopment.dweller.common.game.GameSettings;
import org.roguelikedevelopment.dweller.common.util.logger.Logger;


public class DwellerJ2MEMidlet extends MIDlet implements DwellerApplication {
	
	private DwellerJ2MECanvas canvas = null;
	private GameHandler game = null;
	private RecordStoreStorage rs;
	
	public DwellerJ2MEMidlet() {
	}	
	
	protected void startApp() {
		Logger.debug("DwellerMidlet.startApp() canvas = " + canvas);
		
		if(canvas == null) {				    	
			rs = new RecordStoreStorage();
	    	game = GameHandler.getInstance();
			canvas = new DwellerJ2MECanvas(game);
	    	game.init(this);
		}
		
		Display.getDisplay(this).setCurrent(canvas);
	}
	
	public void setDefaultKeys() {
		GameCommand.initializeCommand(GameCommand.CMD_LOOK, Canvas.KEY_NUM0, Canvas.KEY_NUM0, false, false);
		GameCommand.initializeCommand(GameCommand.CMD_DOWNLEFT, Canvas.KEY_NUM7, Canvas.KEY_NUM7, false, false);
		GameCommand.initializeCommand(GameCommand.CMD_DOWN, Canvas.KEY_NUM8, canvas.getKeyCode(Canvas.DOWN), false, true);
		GameCommand.initializeCommand(GameCommand.CMD_DOWNRIGHT, Canvas.KEY_NUM9, Canvas.KEY_NUM9, false, false);
		GameCommand.initializeCommand(GameCommand.CMD_LEFT, Canvas.KEY_NUM4, canvas.getKeyCode(Canvas.LEFT), false, true);
		GameCommand.initializeCommand(GameCommand.CMD_CENTER, Canvas.KEY_NUM5, canvas.getKeyCode(Canvas.FIRE), false, true);
		GameCommand.initializeCommand(GameCommand.CMD_RIGHT, Canvas.KEY_NUM6, canvas.getKeyCode(Canvas.RIGHT), false, true);
		GameCommand.initializeCommand(GameCommand.CMD_UPLEFT, Canvas.KEY_NUM1, Canvas.KEY_NUM1, false, false);
		GameCommand.initializeCommand(GameCommand.CMD_UP, Canvas.KEY_NUM2, canvas.getKeyCode(Canvas.UP), false, true);
		GameCommand.initializeCommand(GameCommand.CMD_UPRIGHT, Canvas.KEY_NUM3, Canvas.KEY_NUM3, false, false);
		GameCommand.initializeCommand(GameCommand.CMD_INVENTORY, Canvas.KEY_POUND, canvas.getKeyCode(Canvas.GAME_A), false, false);
		GameCommand.initializeCommand(GameCommand.CMD_MENU, Canvas.KEY_STAR, canvas.getKeyCode(Canvas.GAME_B), false, false);
		GameCommand.initializeCommand(GameCommand.CMD_EXIT, Canvas.KEY_STAR, canvas.getKeyCode(Canvas.GAME_B), false, false);
		GameCommand.initializeCommand(GameCommand.CMD_CYCLETARGET, Canvas.KEY_POUND, Canvas.KEY_POUND, false, false);
	}
	
	protected void pauseApp() {
		game.saveGame();
	}
	
	public void destroyApp(boolean unconditional) {
		notifyDestroyed();
	}
	
	public void exit() {
		destroyApp(true);
	}


	public DataStorage getDataStorage() {
		return rs;
	}

	public String getKeyCodeName(int keyCode) {
		return canvas.getKeyCodeName(keyCode);
	}

	public DwellerCanvas getCanvas() {
		return canvas;
	}

	public void settingsChanged(GameSettings settings) {
		if(settings.isUsingGraphics()) {
			canvas.loadGraphics();
		}
		else {
			canvas.loadASCII();
		}
		
		if(settings.isUsingNormalTiles()) {
			canvas.useNormalTiles();
		}
		else {
			canvas.useLargeTiles();
		}
	}

	public DataInputStream getFile(String name) {
		Logger.debug("DwellerMidlet.getFile() " + name);
		return new DataInputStream(getClass().getResourceAsStream("/" + name));
	}
}